<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <title>联系人表</title>
    <script src="../../../static/kaige/js/szload.js"></script>
</head>
<body>
<div class="kaige-body">
    <table class="layui-table" id="pageTable" lay-filter="pageTable" lay-size="sm"></table>
    <input type="hidden" name="keywordsCache" value="{}"/>
</div>
<script src="../../../static/lib/jquery-3.6.0.min.js"></script>
<script>
    let jq = jQuery.noConflict();
    load_init_js([
        szoapcpath + '/static/kaige/js/date.js',
        szoapcpath + '/static/kaige/js/search.js',
        szoapcpath + '/static/kaige/js/xm-select.js'
    ], function () {
        layui.config({
            base: szoapcpath + '/static/kaige/layuiextend/'
        }).use(['element', 'table', 'form', 'layer', 'kaige', 'laydate', 'glfiles', 'laytpl', 'lvybtpl'], function () {
            var element = layui.element,
                layer = layui.layer,
                table = layui.table,
                form = layui.form,
                kaige = layui.kaige,
                laydate = layui.laydate,
                laytpl = layui.laytpl,
                lvybtpl = layui.lvybtpl,
                glfiles = layui.glfiles;

            window.kaige_page = {
                logout_url: '/index/logout',
                welcome: './pages/welcome.html',
                menus: [],
                leftmenu: [],
            };
            window.pagedict = parent.window.kaige_page.dict || {};
            form.render();

            kaige.table({
                elem: '#pageTable',
                url: "/crm.contacts/index",
                id: 'pageTable',
                toolbar: '#dtoolbar',
                defaultToolbar: ['filter'],
                cols: [[
                    {field: 'id', title: '', align: 'center'},
                    {field: 'name', title: '姓名', align: 'center', templet: "#name_click"},
                    {field: 'customer_id_name', title: '所属客户', align: 'center'},
                    {field: 'subname', title: '简称或尊称', align: 'center'},
                    {field: 'role', title: '角色', align: 'center', templet: "#role_name"},
                    {field: 'post', title: '职务', align: 'center'},
                    {field: 'email', title: '邮箱', align: 'center'},
                    {field: 'mobile', title: '手机号', align: 'center'},
                    {field: 'telephone', title: '固定座机', align: 'center'},
                    {field: 'weichat', title: '微信号', align: 'center'},
                    {field: 'remarks', title: '备注信息', align: 'center'},
                    {title: '操作', toolbar: '#dtrbar', align: 'center', width: 150, fixed: 'right'}
                ]]
            });

            //列表-监听表头工具栏事件dtoolbar
            table.on('toolbar(pageTable)', function (obj) {
                switch (obj.event) {
                    case 'btn-add':
                        btnadd();
                        break;
                }
            });
            window.showRecord = function (id, name) {
                console.log(id, name);
                var detailData = {};
                detailData = $.extend(pagedict, {id: id, name: name});
                lvybtpl.ajax({
                    jq: jq,
                    type: 'get',
                    url: szoa.client.path + '/pages/crm/contacts/record.html',
                    id: 'record',
                    offset: 'r',
                    detailData: detailData,
                    postUrl: '/crm.contacts_record/add',
                    callafter: function () {
                        laydate.render({
                            elem: '#next_time'
                            , type: 'date'
                        });

                        $('#reminds_name').on('focus', function () {
                            //rece_id_name 要调用的文本框
                            layer.open({
                                title: '选择适用对象',
                                content: szoa.client.path + '/pages/pmm/config/ugdc.html',
                                area: ['690px', '470px'],
                                type: 2,
                                success: function (layero, index) {
                                    window.ugdcinfo = function (opt) {
                                        $('#reminds_name').val(opt.names);
                                        $('#reminds_id').val(opt.ids); // 隐藏的文本框存 id ,部门id(u1,u4),组id(g1,g2),部门根ID（c1,c9)与部门ID区别 根ID需要查找同所有子部门
                                        layer.close(layer.index);
                                    }
                                }
                            });
                        });

                    },
                    contentTable: 'contentTable',
                });
            }


            window.showNamePage = function (obj) {
                jq.ajax({
                    type: 'get',
                    url: szoa.client.path + '/pages/crm/contacts/name.html',
                    data: {d: obj.id},
                    dataType: 'html',
                    success: function (res) {
                        laytpl(res).render(eval(obj), function (html) {
                            var index2 = layer.open({
                                id: 'name_pahe_form_box',
                                type: 1,
                                anim: 4,
                                area: ['750px', '450px'],
                                title: "详细页面",
                                content: html,
                                btn: ['确定', '取消'],
                                yes: function (index, layero) {
                                    layer.close(index2);
                                },
                                success: function () {
                                    window.editInfo = function (id) {
                                        kaige.form({
                                            url: "/crm.contacts/edit",
                                            data: {id: id},
                                            id: 'kaige',
                                            area: ['750px', '450px'],
                                            title: '<i class="layui-icon layui-icon-edit"></i> 编辑 - 联系人表',
                                            tablelayid: 'pageTable',
                                            overflow: false,
                                            encode: 1,
                                            callbefore: function (layero, index) {
                                                addEditData();
                                            },
                                        });
                                    }
                                    window.delInfo = function (id) {
                                        layer.confirm('您确定删除吗？', {
                                            title: '<i class="layui-icon layui-icon-delete"></i> 删除确认',
                                            btn: ['确认', '取消']
                                        }, function () {
                                            $.post("/crm.contacts/del", {id: id}, function (res) {
                                                res.status && table.reload('pageTable');
                                                layer.msg(res.msg);
                                            });
                                        });
                                    }
                                    window.sendEmail = function (id) {
                                        jq.ajax({
                                            type: 'get',
                                            url: szoa.client.path + '/pages/crm/contacts/email.html',
                                            data: {d: obj.id},
                                            dataType: 'html',
                                            success: function (res) {
                                                laytpl(res).render(eval(obj), function (html) {
                                                    var index3 = layer.open({
                                                        id: 'email_pahe_form_box',
                                                        type: 1,
                                                        anim: 4,
                                                        offset: "r",
                                                        area: ['750px', '450px'],
                                                        title: "发送邮件",
                                                        content: html,
                                                        btn: ['确定', '取消'],
                                                        yes: function (index, layero) {
                                                            $('#email_form_btn_save').click();
                                                        },
                                                        success: function () {
                                                            form.on('submit(email_form_btn_save)', function (data2) {
                                                                $.ajax({
                                                                    encode: 1,
                                                                    type: 'POST',
                                                                    url: "/crm.contacts/sendEmail",
                                                                    data: data2.field,
                                                                    success: function (res2) {
                                                                        if (res2.status) {
                                                                            layer.msg(res2.msg, {time: 1300}, function () {
                                                                                layer.close(index3);
                                                                            });
                                                                        } else {
                                                                            layer.msg(res2.msg);
                                                                        }
                                                                    }
                                                                });
                                                                return false;
                                                            });
                                                        }
                                                    });
                                                })
                                            }
                                        });
                                    }
                                    table.render({
                                        elem: '#contentTable'
                                        , url: "/crm.contacts_record/index"
                                        , id: 'contentTable'
                                        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                                        , cols: [[
                                            {field: 'follow_type_name', title: '跟进类型', align: 'center', width: 100},
                                            {field: 'follow_time', title: '跟进时间', align: 'center'},
                                            {field: 'follow_name', title: '跟进状态', align: 'center'},
                                            {field: 'content', title: '跟进内容', align: 'center'},
                                            {field: 'next_time', title: '下次跟进时间', align: 'center'},
                                        ]]
                                        , where: {
                                            "relation_id": obj.id,
                                        }
                                    });


                                }
                            });
                        });
                    }
                });
            }
            window.show_employee = function () {
                laydate.render({
                    elem: '#next_time'
                    , type: 'datetime'
                });

                kaige.xmSelect({
                    id: 'relation_id',
                    url: '/select/ClueName',
                    udata: {automatch: 1},
                    radio: true,
                    clickClose: true
                });

                $('#reminds_name').on('focus', function () { //rece_id_name 要调用的文本框
                    layer.open({
                        title: '选择适用对象',
                        content: szoa.client.path + '/pages/pmm/config/ugdc.html',
                        area: ['690px', '470px'],
                        type: 2,
                        success: function (layero, index) {
                            window.ugdcinfo = function (opt) {
                                $('#reminds_name').val(opt.names);
                                $('#reminds_id').val(opt.ids); // 隐藏的文本框存 id ,部门id(u1,u4),组id(g1,g2),部门根ID（c1,c9)与部门ID区别 根ID需要查找同所有子部门
                                layer.close(layer.index);
                            }
                        }
                    });
                });
            }
            /**
             * 跟进
             */
            window.recordAction = function (d) {
                kaige.form({
                    url: "/crm.contacts_record/add",
                    data: {},
                    id: 'contacts_record',
                    area: ['750px', '450px'],
                    title: '<i class="layui-icon layui-icon-add-circle"></i> 添加 - 联系人表',
                    tablelayid: 'pageTable',
                    overflow: false,
                    encode: 1,
                    callbefore: function (layero, index) {
                        $("#relation_id").val(d.id);
                        $("#relation_id_name").val(d.name);
                        show_employee();
                    },
                    postbefore: function (updata) {
                        updata.files_id = updata.form_files_id;
                        delete updata.form_files_id;
                        return updata;
                    },
                    getdata: function (res) {
                        var data = (res && res.data && res.data.fi) ? res.data.fi : null;
                        glfiles.form({
                            id: 'form',
                            is_edit: true,
                            data: data
                        });
                    }
                });


            }
            //列表-监听单元格行工具事件dtrbar
            table.on('tool(pageTable)', function (obj) {
                var that = this;
                var data = obj.data;
                switch (obj.event) {
                    case 'edit':
                        baredit(data);
                        break;
                    case 'del':
                        bardel(data);
                        break;
                    case 'record':
                        recordAction(data);
                        break;
                    case 'showNamePage':
                        showNamePage(data);
                        break;
                }
            });
            window.addEditData = function () {
                if ($("#customer_id_select_box").html() != undefined) {
                    kaige.xmSelect({
                        id: 'customer_id',
                        url: '/select/customer',
                        udata: {automatch: 1},
                        radio: true,
                        clickClose: true
                    });
                }
                laydate.render({
                    elem: '#birthday'
                    , type: 'date'
                });
            }

            //添加
            function btnadd() {
                kaige.form({
                    url: "/crm.contacts/add",
                    data: {},
                    id: 'kaige',
                    area: ['750px', '450px'],
                    title: '<i class="layui-icon layui-icon-add-circle"></i> 添加 - 联系人表',
                    tablelayid: 'pageTable',
                    overflow: false,
                    encode: 1,
                    callbefore: function (layero, index) {
                        addEditData();
                    }
                });
            }


            //编辑
            function baredit(d) {
                editInfo(d.id);
            }

            //删除
            function bardel(d) {
                delInfo(d.id);
            };

        });
    });
</script>
<script type="text/html" id="dtrbar">
    <div class="layui-btn-group">
        <!--        <button class="layui-btn layui-btn-xs" lay-event="edit" title="编辑">编辑-->
        <!--        </button>-->
        <button class="layui-btn layui-btn-xs" lay-event="record" title="跟进">跟进
        </button>
        <!--        <button class="layui-btn layui-btn-xs" lay-event="del" title="删除">删除-->
        <!--        </button>-->
    </div>
</script>
<script type="text/html" id="dtoolbar">
    <div class="layui-btn-container layui-form filter-form" lay-filter="searchform">
        <div class="layui-btn-group">
            <button type="button" class="layui-btn layui-btn-sm" lay-event="btn-add" style="margin-top:2px;"><i
                    class="layui-icon layui-icon-add-circle"></i> 添加
            </button>
        </div>
    </div>
</script>
<script type="text/html" id="role_name">
    {{# for(var ipd in pagedict.custome_role){ }}
    {{# if(d.role ==ipd){ }}
    <span>{{ pagedict.custome_role[ipd] }}</span>
    {{# } }}
    {{# } }}
</script>

<script type="text/html" id="name_click">
    <a href="javascript:void(0);" lay-event="showNamePage" style="color:#3c8dbc">{{ d.name }}</a>
</script>

<script type="text/html" id="kaige_form_html">
    <form class="layui-form layui-form-pane kaige-body-form" style="padding: 5px;" lay-filter="kaige_form_lay_filter">
        {{# if(d.id){ }}
        <input type="hidden" name="id" value="{{ d.id }}"/>
        {{# } }}
        <div class="layui-form-item">
            <label class="layui-form-label">客户ID</label>
            <div class="layui-input-inline">
                {{# if(d.id){ }}
                <input type="hidden" id="customer_id" value="{{ d.customer_id || '' }}" name="customer_id"/>
                <input type="text" id="customer_id_name" class="layui-input" value="{{ d.customer_id_name || '' }}"
                       name="customer_id_name" readonly/>
                {{# }else{ }}
                <div id="customer_id_select_box"></div>
                <input type="hidden" id="customer_id" value="{{ d.customer_id || '' }}" name="customer_id"/>
                <input type="hidden" id="customer_id_name" value="{{ d.customer_id_name || '' }}"
                       name="customer_id_name"/>
                {{# } }}
            </div>
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input name="name" value="{{ d.name || '' }}" class="layui-input" type="text">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">简称或尊称</label>
            <div class="layui-input-inline">
                <input name="subname" value="{{ d.subname || '' }}" class="layui-input" type="text">
            </div>
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input name="mobile" value="{{ d.mobile || '' }}" class="layui-input" type="text"
                       lay-verify="required|mobile">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-inline">
                <select name="role" lay-verify="required">
                    {{# lay.each(pagedict.custome_role,function(index,item){
                    if(index == ''){
                    top.layer.msg('系统字典->跟进类型未配置完成，值不能为空');
                    }
                    }}
                    <option value="{{ index }}" {{ d.role== index ?
                    'selected' :'' }}>{{ item }}</option>
                    {{# }) }}
                </select>
            </div>
            <label class="layui-form-label">部门职务</label>
            <div class="layui-input-inline">
                <input name="post" value="{{ d.post || '' }}" class="layui-input" type="text">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">固定电话</label>
            <div class="layui-input-inline">
                <input name="telephone" value="{{ d.telephone || '' }}" class="layui-input" type="text">
            </div>

            <label class="layui-form-label">生日</label>
            <div class="layui-input-inline">
                <input id="birthday" name="birthday" value="{{ d.birthday || '' }}" class="layui-input" type="text">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline">
                <input name="email" value="{{ d.email || '' }}" class="layui-input" type="text">
            </div>

            <label class="layui-form-label">微信</label>
            <div class="layui-input-inline">
                <input name="weichat" value="{{ d.weichat || '' }}" class="layui-input" type="text">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <input name="remarks" value="{{ d.remarks || '' }}" class="layui-input" type="text">
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <button class="layui-hide" lay-submit="" id="kaige_form_btn_save" lay-filter="kaige_form_btn_save">保存数据
            </button>
        </div>
    </form>
</script>


<script type="text/html" id="contacts_record_form_html">
    <form class="layui-form layui-form-pane kaige-body-form" style="padding: 5px;"
          lay-filter="contacts_record_form_lay_filter">
        <input type="hidden" id="relation_id" value="" name="relation_id"/>
        <input type="hidden" id="relation_id_name" value="" name="relation_name"/>
        <div class="layui-form-item">
            <label class="layui-form-label">跟进类型</label>
            <div class="layui-input-inline">
                <select name="follow_type" lay-verify="required">
                    {{# lay.each(pagedict.followType,function(index,item){
                    if(index == ''){
                    top.layer.msg('系统字典->跟进类型未配置完成，值不能为空');
                    }
                    }}
                    <option value="{{ index }}" {{ d.follow_type== index ?
                    'selected' :'' }}>{{ item }}</option>
                    {{# }) }}
                </select>
            </div>

            <label class="layui-form-label">下次跟进时间</label>
            <div class="layui-input-inline">
                <input id="next_time" name="next_time" value="{{ d.next_time || '' }}" class="layui-input" type="text">
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">跟进内容</label>
            <div class="layui-input-block">
                <input name="content" value="{{ d.content || '' }}" class="layui-input" type="text">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">跟进状态</label>
            <div class="layui-input-inline">
                <select name="follow" lay-verify="required">
                    {{# lay.each(pagedict.followStatus,function(index,item){
                    if(index == ''){
                    top.layer.msg('系统字典->客户跟进状态未配置完成，值不能为空');
                    }
                    }}
                    <option value="{{ index }}" {{ d.follow== index ?
                    'selected' :'' }}>{{ item }}</option>
                    {{# }) }}
                </select>
            </div>
            <label class="layui-form-label">提醒人员列表</label>
            <div class="layui-input-inline">
                <input id="reminds_name" type="text" value="{{ d.reminds_name || '' }}" name="reminds_name"
                       class="layui-input"/>
                <input id="reminds_id" type="hidden" value="{{ d.reminds_id || '' }}" name="reminds_id"
                       class="layui-input"/>
            </div>
        </div>

        <div class="layui-form-item" id="form_files_gl_html"></div>

        <div class="layui-form-item layui-hide">
            <button class="layui-hide" lay-submit="" id="contacts_record_form_btn_save"
                    lay-filter="contacts_record_form_btn_save">保存数据
            </button>
        </div>
    </form>
</script>

</body>

</html>